package edu.zju.jzs.wc.entity;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

import java.io.*;

/**
 * Created by 11544 on 2017/10/12.
 */
public class Test {


    public static void main(String[] args) throws  Exception
    {
        ByteOutputStream ba=new ByteOutputStream();
        ByteOutputStream ba2=new ByteOutputStream();
        //定义两个DataOutputStream，用于将普通对象进行jdk标准序列化
        DataOutputStream dout = new DataOutputStream(ba);
        DataOutputStream dout2 = new DataOutputStream(ba2);
        ObjectOutputStream obout = new ObjectOutputStream(dout2);
        //定义两个bean，作为序列化的源对象
        ItemBeanSer itemBeanSer = new ItemBeanSer(1000L, 89.9f);
        ItemBean itemBean = new ItemBean(1000L, 89.9f);

        //用于比较String类型和Text类型的序列化差别
        Text atext = new Text("a");
        // atext.write(dout);
        itemBean.write(dout);

        byte[] byteArray = ba.toByteArray();

        //比较序列化结果
        System.out.println(byteArray.length);
        for (byte b : byteArray) {

            System.out.print(b);
            System.out.print(":");
        }

        System.out.println("-----------------------");

        String astr = "a";
        // dout2.writeUTF(astr);
        obout.writeObject(itemBeanSer);

        byte[] byteArray2 = ba2.toByteArray();
        System.out.println(byteArray2.length);
        for (byte b : byteArray2) {
            System.out.print(b);
            System.out.print(":");
        }
    }

    private static class ItemBeanSer implements Serializable{
        private long longdata;
        private float floatdata;

        ItemBeanSer(long longdata,float floatdata)
        {
            this.longdata=longdata;
            this.floatdata=floatdata;
        }


    }

    private static class ItemBean implements WritableComparable{

        private LongWritable longdata;
        private FloatWritable floatdata;

        ItemBean(long longdata,float floatdata)
        {
            this.longdata=new LongWritable(longdata);
            this.floatdata=new FloatWritable(floatdata);
        }

        public int compareTo(Object o) {
            if(o instanceof ItemBean)
            return 10;

            else
                return 0;
        }

        public void write(DataOutput dataOutput) throws IOException {
        System.out.println("the text write");
        dataOutput.writeLong(this.longdata.get());
        dataOutput.writeFloat(this.floatdata.get());
        }

        public void readFields(DataInput dataInput) throws IOException {
        System.out.println("the text read");
        }
    }
}
